using System;
// <copyright file="ServerExplorerWindow.cs" company="DotNetSide">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>fabio.cozzolino</author>
// <date>30/06/2008 18.09.35</date>

namespace DotNetSide.SqlScriptGenerator
{
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using WeifenLuo.WinFormsUI.Docking;
    using System.Data.SqlClient;
    using DotNetSide.SqlScriptGenerator.Properties;

    public partial class ServerExplorerWindow : DockContent
    {
        private ImageList imageList;
        private TreeView serversExplorer;

        private string[] servers;
        public string[] Servers
        {
            get { return this.servers; }
            set { this.servers = value; }
        }

        private AuthenticationMethods authenticationMethod;
        public AuthenticationMethods AauthenticationMethod
        {
            get { return this.authenticationMethod; }
            set { this.authenticationMethod = value; }
        }

        private string username;
        public string Username
        {
            get { return this.username; }
            set { this.username = value; }
        }

        private string password;
        public string Password
        {
            get { return this.password; }
            set { this.password = value; }
        }

        public delegate void DataBaseChanged(object sender, string serverName, string databaseName);
        public event DataBaseChanged OnDataBaseChanged;

        public ServerExplorerWindow()
        {
            InitializeComponent();

            this.imageList = new ImageList();
            this.imageList.Images.Add("database", Resources.database);
            this.imageList.Images.Add("server", Resources.server);
            this.imageList.Images.Add("table", Resources.table);

            this.serversExplorer = new TreeView();
            this.serversExplorer.Dock = DockStyle.Fill;
            this.serversExplorer.ImageList = this.imageList;
            this.serversExplorer.NodeMouseClick += new TreeNodeMouseClickEventHandler(serversExplorer_NodeMouseClick);
            this.ServerPanel.Controls.Add(serversExplorer);
        }

        protected override void OnRightToLeftLayoutChanged(EventArgs e)
        {
            //treeView1.RightToLeftLayout = RightToLeftLayout;
        }

        void serversExplorer_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            string databaseName = (string)e.Node.Tag;
            if (databaseName == "DatabaseName" && OnDataBaseChanged != null)
            {
                OnDataBaseChanged(this, servers[0], e.Node.Text);
            }
        }

        private void LoadServer(string serverName)
        {
            string connectionString = this.GetConnectionString(serverName);
            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();
            DataTable schema = conn.GetSchema("Databases");
            conn.Close();

            TreeNode serverNode = this.serversExplorer.Nodes.Add(serverName);
            serverNode.ImageKey = "server";
            foreach (DataRow database in schema.Rows)
            {
                string databaseName = (string)database[0];
                TreeNode databaseNode = serverNode.Nodes.Add(databaseName);
                databaseNode.Tag = "DatabaseName";
                databaseNode.ImageKey = "database";
                //databaseNode.Nodes.AddRange(LoadTables(serverName, databaseName));
            }
        }

        public string GetConnectionString(string serverName)
        {
            string connectionString;

            if (this.authenticationMethod == AuthenticationMethods.Windows)
            {
                connectionString = "Data Source=" + serverName + ";Integrated Security=SSPI";
            }
            else
            {
                connectionString = "Data Source=" + serverName + ";User ID=" + this.username + ";Password=" + this.password;
            }

            return connectionString;
        }

        public string GetConnectionString(string serverName, string databaseName)
        {
            string connectionString;

            if (this.authenticationMethod == AuthenticationMethods.Windows)
            {
                connectionString = "Data Source=" + serverName + ";Initial Catalog=" + databaseName + ";Integrated Security=SSPI";
            }
            else
            {
                connectionString = "Data Source=" + serverName + ";Initial Catalog=" + databaseName + ";User ID=" + this.username + ";Password=" + this.password;
            }

            return connectionString;
        }

        private TreeNode[] LoadTables(string server, string database)
        {
            string connectionString = this.GetConnectionString(server);
            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();
            DataTable schema = conn.GetSchema("Tables", new string[] { database, null, null, "BASE TABLE" });
            conn.Close();

            List<TreeNode> tableNodes = new List<TreeNode>();
            foreach (DataRow row in schema.Rows)
            {
                string tableName = (string)row.ItemArray[2];
                tableNodes.Add(new TreeNode(tableName));
            }
            return tableNodes.ToArray();
        }

        private void ServerExplorerForm_Load(object sender, EventArgs e)
        {
            this.LoadServer(this.servers[0]);
        }

        private void NewServerToolStripButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not yet implemented");
        }
    }
}